using UniversalAdmin.Application.Services;

namespace UniversalAdmin.Infrastructure.Services
{
    /// <summary>
    /// 密码哈希服务实现 - 使用 BCrypt
    /// </summary>
    public class PasswordHasher : IPasswordHasher
    {
        /// <summary>
        /// 哈希密码
        /// </summary>
        /// <param name="password">明文密码</param>
        /// <returns>哈希后的密码</returns>
        public string Hash(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
                throw new ArgumentException("Password cannot be null or empty", nameof(password));

            // 使用 BCrypt 进行密码哈希
            return BCrypt.Net.BCrypt.HashPassword(password, BCrypt.Net.BCrypt.GenerateSalt(12));
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="password">明文密码</param>
        /// <param name="hashedPassword">哈希密码</param>
        /// <returns>是否匹配</returns>
        public bool Verify(string password, string hashedPassword)
        {
            if (string.IsNullOrWhiteSpace(password))
                return false;
            
            if (string.IsNullOrWhiteSpace(hashedPassword))
                return false;

            try
            {
                return BCrypt.Net.BCrypt.Verify(password, hashedPassword);
            }
            catch
            {
                return false;
            }
        }
    }
}
